/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.service.transaction.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.grayrabbit.pay.core.model.pages.PageList;
import org.grayrabbit.pay.core.model.pages.PageTools;
import org.grayrabbit.pay.core.service.transaction.TradeService;
import org.grayrabbit.pay.core.dao.transaction.TradeDao;
import org.grayrabbit.pay.core.model.entity.transaction.Trade;

/**
 * 
 * 
 * @author liyd
 * @version $Id: TradeServiceImpl.java, v 0.1 Fri Jun 01 11:16:04 CST 2012 liyd
 *          Exp $
 */
@Component
public class TradeServiceImpl implements TradeService {

    @Autowired
    private TradeDao tradeDao;

    /**
     * 查询业务对象列表
     * 
     * @param trade
     * @return
     */
    public PageList<Trade> queryPageList(Trade trade, Date startDate, Date endDate) {

        PageTools pageTools = new PageTools();
        pageTools.setPage(trade.getPageNum());
        pageTools.setItemsPerPage(trade.getPageSize());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("query", trade);
        params.put("startRow", pageTools.getBeginIndex());
        params.put("pageSize", pageTools.getItemsPerPage());
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        pageTools.setItems(tradeDao.queryCount(params));

        PageList<Trade> pageList = new PageList<Trade>();
        pageList.addAll(tradeDao.queryPageList(params));
        pageList.setPageTools(pageTools);

        return pageList;

    }

    public PageList<Trade> queryPageList(Trade trade) {

        PageTools pageTools = new PageTools();
        pageTools.setPage(trade.getPageNum());
        pageTools.setItemsPerPage(trade.getPageSize());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("query", trade);
        params.put("startRow", pageTools.getBeginIndex());
        params.put("pageSize", pageTools.getItemsPerPage());
        pageTools.setItems(tradeDao.queryCount(params));

        PageList<Trade> pageList = new PageList<Trade>();
        pageList.addAll(tradeDao.queryPageList(params));
        pageList.setPageTools(pageTools);

        return pageList;

    }

    public int updateStatus(String fromStatus, String toStatus) {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("fromStatus", fromStatus);
        params.put("toStatus", toStatus);
        return tradeDao.updateStatus(params);

    }

    /**
     * 根据Id得到实体对象
     * 
     * @param id
     * @return
     */
    public Trade getById(Long id) {

        return tradeDao.getById(id);
    }

    /**
     * 根据id批量删除
     * 
     * @param idArray
     */
    public void deleteBatchById(Long[] idArray) {
        tradeDao.deleteBatchById(idArray);
    }

    /**
     * 添加一个对象
     * 
     * @param trade
     */
    public void insert(Trade trade) {

        tradeDao.save(trade);

    }

    /**
     * 更新一个对象
     * 
     * @param trade
     * @return
     */
    public int update(Trade trade) {

        return tradeDao.update(trade);

    }

    public List<Trade> findByStatus(String status) {
        return tradeDao.findByStatus(status);
    }

    /**
     * 删除一个对象
     * 
     * @param trade
     */
    public void delete(Trade trade) {
        tradeDao.delete(trade);
    }

    @Override
    public List<Trade> getBySystemId(Long systemId) {
        // TODO Auto-generated method stub
        return tradeDao.getBySystemId(systemId);
    }

    /**
     * 根据trade查询交易列表
     * 
     * @param trade
     * @return
     */
    public List<Trade> queryList(Trade trade) {
        return tradeDao.queryList(trade);
    }

}
